//
//  WetherInfoVO.m
//  GolfFriend
//
//  Created by Vescky on 13-11-26.
//  Copyright (c) 2013年 vescky.org. All rights reserved.
//

#import "WetherInfoVO.h"
#import <objc/runtime.h>

@implementation WetherInfoVO
@synthesize cityId,cityName,cityNameEn;
@synthesize sixDaysTemp,sixDaysWeather;
@synthesize currentTemp,currentWeather,currentWeatherIcon;

- (id)init{
    self = [super init];
    return self;
}

- (id)copyWithZone:(NSZone *)zone {
    id copy = [[[self class] alloc] init];
    if (copy) {
        [copy setCityId:self.cityId];
        [copy setCityName:self.cityName];
        [copy setCityNameEn:self.cityNameEn];
        [copy setSixDaysTemp:self.sixDaysTemp];
        [copy setSixDaysWeather:self.sixDaysWeather];
        [copy setCurrentTemp:self.currentTemp];
        [copy setCurrentWeather:self.currentWeather];
        [copy setCurrentWeatherIcon:self.currentWeatherIcon];
    }
    return copy;
}

- (NSDictionary *)toDictionary
{
    NSMutableDictionary *dictionaryFormat = [NSMutableDictionary dictionary];
    //  取得当前类类型
    Class cls = [self class];
    
    unsigned int ivarsCnt = 0;
    //　获取类成员变量列表，ivarsCnt为类成员数量
    Ivar *ivars = class_copyIvarList(cls, &ivarsCnt);
    
    //　遍历成员变量列表，其中每个变量都是Ivar类型的结构体
    for (const Ivar *p = ivars; p < ivars + ivarsCnt; ++p)
    {
        Ivar const ivar = *p;
        //　获取变量名
        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
        // 若此变量未在类结构体中声明而只声明为Property，则变量名加前缀 '_'下划线
        // 比如 @property(retain) NSString *abc;则 key == _abc;
        //　获取变量值
        id value = [self valueForKey:key];
        //　取得变量类型
        // 通过 type[0]可以判断其具体的内置类型
        //        const char *type = ivar_getTypeEncoding(ivar);
        if (value)
        {
            [dictionaryFormat setObject:value forKey:key];
        }
    }
    
    return dictionaryFormat;
    
}

- (void)dictionaryToWeatherInfoVO:(NSDictionary*)dict {
//    id copy = [[[self class] alloc] init];
//    if (copy) {
//        [copy setCityId:[dict objectForKey:@"cityId"]];
//        [copy setCityName:[dict objectForKey:@"cityName"]];
//        [copy setCityNameEn:[dict objectForKey:@"cityNameEn"]];
//        [copy setSixDaysTemp:[dict objectForKey:@"sixDaysTemp"]];
//        [copy setSixDaysWeather:[dict objectForKey:@"sixDaysWeather"]];
//        [copy setCurrentTemp:[dict objectForKey:@"currentTemp"]];
//        [copy setCurrentWeather:[dict objectForKey:@"currentWeather"]];
//        [copy setCurrentWeatherIcon:[dict objectForKey:@"currentWeatherIcon"]];
//    }
//    return copy;
    if (![dict isKindOfClass:[NSDictionary class]] || !dict) {
        return;
    }
    [self setCityId:[dict objectForKey:@"cityId"]];
    [self setCityName:[dict objectForKey:@"cityName"]];
    [self setCityNameEn:[dict objectForKey:@"cityNameEn"]];
    [self setSixDaysTemp:[dict objectForKey:@"sixDaysTemp"]];
    [self setSixDaysWeather:[dict objectForKey:@"sixDaysWeather"]];
    [self setCurrentTemp:[dict objectForKey:@"currentTemp"]];
    [self setCurrentWeather:[dict objectForKey:@"currentWeather"]];
    [self setCurrentWeatherIcon:[dict objectForKey:@"currentWeatherIcon"]];
    
}

@end
